package com.xms.schema.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.schema.core.AttributeDefaults;
import com.xms.schema.core.AttributeTypeIds;
import com.xms.schema.entity.Attribute;
import com.xms.schema.entity.Entity;
import com.xms.schema.service.attribute.IAttributeFinderService;
import com.xms.schema.service.attribute.IAttributeUpdaterService;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.security.core.PrivilegeResourceItem;
import com.xms.utils.CollectionUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import com.xms.web.framework.model.UpdateAuthorizationStateModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * AttributeAuthorizationController
 * </p>
 *
 * @author migo
 * @since 2021/10/19
 */
@RestController
@RequestMapping("{org}/api/schema/attribute")
public class AttributeAuthorizationController extends ApiBaseController {
    private IEntityFinderService _entityFinderService;
    private IAttributeFinderService _attributeFinderService;
    private IAttributeUpdaterService _attributeUpdaterService;
    @Autowired
    public void AttributeAuthorizationController(IEntityFinderService entityFinderService,
                                                 IAttributeFinderService attributeFinderService,
                                                 IAttributeUpdaterService attributeUpdaterService) {
        _entityFinderService = entityFinderService;
        _attributeFinderService = attributeFinderService;
        _attributeUpdaterService = attributeUpdaterService;
    }

    @ApiOperation("查询字段权限资源")
    @RequestMapping(value = "privilegeresource", method = RequestMethod.GET)
    public JsonResultObject privilegeResource(@RequestParam(value = "authorizationenabled", required = false) Boolean authorizationEnabled)
    {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<Attribute>()
                .eq(Attribute::getIscustomizable, true)
                .ne(Attribute::getAttributetypename, AttributeTypeIds.PRIMARYKEY)
                .notIn(Attribute::getName, AttributeDefaults.SYSTEM_ATTRIBUTES);
        if (authorizationEnabled != null)
        {
            queryWrapper.eq(Attribute::getAuthorizationenabled, authorizationEnabled);
        }
        queryWrapper.orderByAsc(Attribute::getName);
        List<Attribute> data = _attributeFinderService.list(queryWrapper);
        if (CollectionUtil.notEmpty(data))
        {
            List<PrivilegeResourceItem> result = new ArrayList<>();
            List<Entity> entities = _entityFinderService.all();
            for (Entity item : entities)
            {
                List<Attribute> attributes = data.stream().filter(x -> x.getEntityid().equals(item.getEntityid())).collect(Collectors.toList());
                if (CollectionUtil.isEmpty(attributes))
                {
                    continue;
                }
                PrivilegeResourceItem group1 = new PrivilegeResourceItem()
                {{
                    label = item.getLocalizedname();
                    children = attributes.stream().map(x -> (new PrivilegeResourceItem() {{ id = x.getAttributeid(); label = x.getLocalizedname(); authorizationenabled = x.getAuthorizationenabled(); }})).collect(Collectors.toList());
                }};
                result.add(group1);
            }
            return JOk(result);
        }
        return JOk();
    }

    @ApiOperation("启用字段权限")
    @PostMapping("authorizationenabled")
    public JsonResultObject authorizationEnabled(@RequestBody UpdateAuthorizationStateModel model)
    {
        List<Attribute> authorizations = _attributeFinderService.getAuthorizations();
        if (CollectionUtil.notEmpty(authorizations))
        {
            _attributeUpdaterService.updateAuthorization(false, authorizations.stream().map(x -> x.getAttributeid()).collect(Collectors.toList()));
        }
        if (CollectionUtil.notEmpty(model.objectId))
        {
            _attributeUpdaterService.updateAuthorization(true, model.objectId);
        }
        return JSaveSuccess();
    }
}
